#include "fuel_refund.hpp"
#include "csv_struct.hpp"

/*bool fuel_refund_csv::exist(int _id)
{
	fuel_refund_struct_csv r;
	return find(_id, r);
}*/

/*bool fuel_refund_csv::exist(int _id_range_ownership, int _id_fuel)
{
	fuel_refund_struct_csv r;
	
	return find(_id_range_ownership, _id_fuel, r);
}*/


float fuel_refund_csv::find_fuel_refund_unit(int _fuel_refund_id, date& _travelling_date)
{
	fuel_refund_struct_csv r(find(_fuel_refund_id, _travelling_date));
	float amount(r.amount());
		
	return amount;
}

float fuel_refund_csv::find_fuel_refund_unit(int _power_range_ownership_id, int _fuel_id, date& _travelling_date)
{
	fuel_refund_struct_csv r(find(_power_range_ownership_id, _fuel_id, _travelling_date));
	float amount(r.amount());

	std::cout << "[fuel_refund_csv::find_fuel_refund_unit] unit refund amount = " << r.amount() << std::endl;

	return amount; 
}


fuel_refund_struct_csv const fuel_refund_csv::find(int _id, date& _date)
{
	csv_struct csv_file(file_list::fuel_refund_filename());
	date date_null(not_a_date_time);

	while(!csv_file.eof())
	{
		++csv_file;
		fuel_refund_struct_csv row(csv_file.get_line());

		if(row.id() != _id) continue;

		//if(row.fromDate() <= _date || row.fromDate() == date_null)) // find first valid row for the specified date 
		if(row.fromDate() > _date && row.fromDate() != date_null) continue; // find first valid row for the specified date 
	
		return row;
	}
	
	fuel_refund_struct_csv fuel_refund_struct_null;
	return fuel_refund_struct_null;
}

fuel_refund_struct_csv const fuel_refund_csv::find(int _id_range_ownership, int _id_fuel, date _date)
{
	csv_struct csv_file(file_list::fuel_refund_filename());
	date date_null(not_a_date_time);
	
	while(!csv_file.eof())
	{
		++csv_file;
		fuel_refund_struct_csv row(csv_file.get_line());
		
		if(row.range_id() != _id_range_ownership) continue;
		if(row.fuel_id() != _id_fuel) continue;
		if(row.toDate() != date_null && row.toDate() < _date) continue;
		if(row.fromDate() > _date && row.fromDate() != date_null) continue;			

		return row;
					
	}	
	
	fuel_refund_struct_csv fuel_refund_struct_null;
	return fuel_refund_struct_null; 
	
}

